Unit myDebug;{$mode objfpc}

Interface

Uses Classes, FileUtil, SysUtils;

Type
    TLogClass = Class
    Private
        FLogLevel: Integer;
        FFileName: WideString;
    Public
        Constructor Create(ALogLevel: Integer = -1);
        Procedure Out(Text: String; Level: Integer);
        Property Level: Integer read FLogLevel write FLogLevel;
    End;
Var
    DebugLog: TLogClass;

Implementation

Constructor TLogClass.Create(ALogLevel: Integer);
Begin
    FLogLevel := ALogLevel;
    FFileName := ChangeFileExt(ParamStrUTF8(0), '.log');
End;

Procedure TLogClass.Out(Text: String; Level: Integer);
Var
    DebugList: TStringList;
Begin
    If (Level <= FLogLevel) Then
    Begin
        DebugList := TStringList.Create;
        If not FileExistsUTF8(FFileName) Then DebugList.SaveToFile(FFileName);
        DebugList.LoadFromFile(FFileName);
        DebugList.Insert(0, FormatDateTime('yyyy"-"mm"-"dd" "HH":"NN":"SS":"zzz',
            Now) + '[' + Text + ']');
        DebugList.SaveToFile(FFileName);
        DebugList.Free;
    End;
End;

End.
